package com.lsp.server.product.service;

/**
 * @ClassName ProductService
 * @Description 商品分类业务层
 * @Author Mr Hao
 * @Date 2021/11/21 13:38
 */

import com.google.gson.JsonArray;
import com.lsp.server.product.domain.po.ProductAttrValues;
import com.lsp.server.product.domain.po.ProductType;
import com.lsp.server.product.domain.vo.AttrVo;
import com.lsp.server.product.mapper.AttrMapper;
import com.lsp.server.product.mapper.TypeMapper;
import com.lsp.server.product.mapper.ValuesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class TypeService {

    @Autowired
    private TypeMapper productMapper;

    private StringBuffer sb =null;

    @Resource
    private AttrMapper attrMapper;

    @Resource
    private ValuesMapper valuesMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *
     * @return
     */
    public Object getlist() {
        sb=new StringBuffer(); //最终的json字符串  [{"id":1,"pid":0,"label":"飞狐电商","children":[{},{}]}]
        // 查询所有的数据
        List<ProductType> types = productMapper.queryAllData();
        //得到根节点数据
        sb.append("[");
        for (ProductType t :types) {
            if(t.getId().equals(1)){
                //得到根节点  得到所有数据的字符串
                sb.append("{\"id\":").append(t.getId()).append(",\"pid\":").append(t.getPid()).append(",\"label\":\"").append(t.getName()).append("\"");
                getCildren(t,types);
                sb.append("}");
                break;
            }
        }
        sb.append("]");
        return sb.toString();
    }

    private void getCildren(ProductType type, List<ProductType> datas) {
        sb.append(",\"children\":").append("[");
        boolean flag=false;
        for (ProductType t:datas) {
            //判断当前数据是否为当前节点的子节点
            if(t.getPid().equals(type.getId())){
                sb.append("{\"id\":").append(t.getId()).append(",\"pid\":").append(t.getPid()).append(",\"label\":\"").append(t.getName()).append("\"");
                getCildren(t,datas);
                sb.append("},");
                flag=true;
            }
        }
        // 判断去掉多余的逗号  还是去掉children    sb 当前就是 ,children:[{},{},
        if(flag){ //去逗号
            sb.replace(sb.length()-1,sb.length(),"]");
        }else{ // ,children:[
            sb.replace(sb.length()-13,sb.length(),"");
        }
    }

    /**
     * 新增
     * @param form
     */
    public void addType(ProductType form) {
        form.setCreateDate(new Date());
        productMapper.addType(form);
    }

    public Map queryTypeAttrDatas(Integer tid) {
        /*构建返回值*/
        Map rs= new HashMap();
        //声明sku和非sku
        List<AttrVo> skuData=new ArrayList<>();
        List<AttrVo> attrData=new ArrayList<>();

        //查询所有的属性
        List<AttrVo> attrs = attrMapper.queryAllData(tid);
        /*  遍历所有属性数据  完善值数据 */
        for (int i = 0; i < attrs.size(); i++) {
            AttrVo attr = attrs.get(i);
            // 根据属性id 查询值
            List<ProductAttrValues> attrValues = valuesMapper.queryAllData(attr.getId());
            //设置默认值
            if(attr.getTypes().equals(2)){
                attr.setVals(new ArrayList<>());
            }else{
                attr.setVals("");
            }
            attr.setValues(attrValues);


            if(attr.getIsSku()==0){
                attrData.add(attr);
            }else{
                skuData.add(attr);
            }
        }
        rs.put("skuData",skuData);
        rs.put("attrData",attrData);
        return rs;
    }

    /*将数据同步到redis*/
    public int refresh() {
        redisTemplate.delete("typesMrHao");
        List<ProductType> types = productMapper.refresh();
        redisTemplate.opsForValue().set("typesMrHao",types);
        return 1;
    }

}
